//
//  NetworkFailView.m
//  Auction
//
//  Created by Gary on 15/9/6.
//  Copyright (c) 2015年 Lebo. All rights reserved.
//

#import "NetworkFailView.h"
#import "Common.h"

static const CGFloat kImageViewWidth         = 100.f;//图片宽度
static const CGFloat kImageViewHeight        = 100.f;//图片高度

static const float kLabelFontSize            = 15.f;//文字大小
#define kLabelTextColor MakeColor(153.f, 153.f, 153.f)  //文字颜色
static const CGFloat kSubviewMaginHorizontal = 15.f;//文字两边的间隔
static const CGFloat kLabelMaginTop          = 30.f;//文字离上方图片的间隔

@interface NetworkFailView () {

}

@end

@implementation NetworkFailView

- (void)initView {
    self.backgroundColor = [UIColor whiteColor];
    if (!_bgView) {
        self.bgView = [[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_bgView];
        
        self.imageView = [[UIImageView alloc] initWithFrame:MakeRect(0, 0, kImageViewWidth, kImageViewHeight)];
        [self addSubview:_imageView];
        _imageView.image = [UIImage imageNamed:@"network_fail_car"];
        
        self.label = [[UILabel alloc] initWithFrame:self.bounds];
        [self addSubview:_label];
        
        _label.textAlignment = NSTextAlignmentCenter;
        _label.numberOfLines = 0;
        _label.text = [Common getLocalStr:@"NetworkResponseFailedToRefresh"];
        _label.font = MakeFont(kLabelFontSize);
        _label.textColor = kLabelTextColor;
        _label.frame = MakeRect(kSubviewMaginHorizontal, 0, ViewWidth(self) - 2 * kSubviewMaginHorizontal, ViewHeight(self) / 2);
        [_label sizeToFit];
        
        //重新设置子视图的位置
        //_imageView.center = CGPointMake(self.center.x, self.center.y - (ViewHeight(_imageView) / 2));
        _imageView.center = CGPointMake(self.center.x, [NetworkFailView defaultImageOrightY]);
        _label.frame = MakeRect(kSubviewMaginHorizontal, ViewBottom(_imageView) + kLabelMaginTop, ViewWidth(self) - 2 * kSubviewMaginHorizontal, ViewHeight(_label));
        
        //添加点击事件
        UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        [self addGestureRecognizer:gr];
    }
}

/**
 *  获取默认图片的垂直位置
 *
 *  @return
 */
+ (CGFloat)defaultImageOrightY {
    return (ScreenHeight - kNavigationBarHeight - kImageViewHeight) / 2;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [self initView];
}

#pragma mark - 事件响应

- (void)handleTap:(UIGestureRecognizer*)gr {
    if (self.delegate && [self.delegate respondsToSelector:@selector(retryNetworkRequest)]) {
        [self.delegate retryNetworkRequest];
    }
    
    [self removeFromSuperview];
}

@end
